import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularExpressionReplace {
	public static void main(String[] s) throws Exception {

		@SuppressWarnings("unused")
		// String title = "";
		String file = "e:/temp/cnnn.txt";
		FileInputStream input = new FileInputStream(file);
		byte[] buf = new byte[256];
		int count;
		StringBuffer sb = new StringBuffer();
		while ((count = input.read(buf)) != -1) {
			sb.append(new String(buf, 0, count));
		}

		String str = sb.toString();
		RegularExpressionReplace t = new RegularExpressionReplace();
		t.filterResourceContent(str);
		System.out.println("-------------------------");
		System.out.println(str);

	}

	private List<String[]> regs = new ArrayList<String[]>();
	{
		regs.add(new String[] { "(?i)<html(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/html>", "" });
		regs.add(new String[] { "(?i)<body(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/body>", "" });

		regs.add(new String[] { "(?i)<div(.*?)>", "<p>" });
		regs.add(new String[] { "(?i)<\\/div>", "</p>" });

		regs.add(new String[] { "(?i)<table(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/table>", "" });
		regs.add(new String[] { "(?i)<caption(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/caption>", "" });
		regs.add(new String[] { "(?i)<thead(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/thead>", "" });
		regs.add(new String[] { "(?i)<tbody(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/tbody>", "" });
		regs.add(new String[] { "(?i)<tfoot(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/tfoot>", "" });
		regs.add(new String[] { "(?i)<tr(.*?)>", "<p>" });
		regs.add(new String[] { "(?i)<\\/tr>", "</p>" });
		regs.add(new String[] { "(?i)<th(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/th>", "<br />" });
		regs.add(new String[] { "(?i)<td(.*?)>", "" });
		regs.add(new String[] { "(?i)<\\/td>", "<br />" });
		regs.add(new String[] { "(?i)<head([^>]+)?>([^\\v]*?)<\\/head>", "" });
		regs.add(new String[] { "(?i)<script([^>]+)?>([^\\v]*?)<\\/script>", "" });
		regs.add(new String[] { "(?i)<style([^>]+)?>([^\\v]*?)<\\/style>", "" });
		regs.add(new String[] { "(?i)(<!doctype((.|\n)*?)>)\n?", "" });
		regs.add(new String[] { "(?i)class=([^ |>]*)([^>])", "" });

		regs.add(new String[] { "(?i)style=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)(<input((.|\n)*?)>)\n?", "" });
		regs.add(new String[] { "(?i)<\\/input>", "" });
		regs.add(new String[] { "(?i)<textarea([^>]+)?>([^\\v]*?)<\\/textarea>", "" });
		regs.add(new String[] { "(?i)<select([^>]+)?>([^\\v]*?)<\\/select>", "" });
		regs.add(new String[] { "(?i)<style([^>]+)?>([^\\v]*?)<\\/style>", "" });

		regs.add(new String[] { "(?i)onblur=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onclick=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)ondblclick=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onfocus=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onkeydown=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onkeypress=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onkeyup=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onmousedown=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onmousemove=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onmouseout=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onmouseover=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onmouseup=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onerror=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onload=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onresize=([^ |>]*)([^>])", "" });
		regs.add(new String[] { "(?i)onunload=([^ |>]*)([^>])", "" });
	}

	public String filterResourceContent(String str) {
		Pattern pattern = null;

		String string = str;
		StringBuffer result;

		for (String[] reg : regs) {
			result = new StringBuffer();
			if (str.trim() != "" && !str.trim().equals("")) {
				pattern = Pattern.compile(reg[0]);
				Matcher matcher = pattern.matcher(string);
				while (matcher.find()) {
					String group = matcher.group();
					matcher.appendReplacement(result, reg[1]);
				}
				matcher.appendTail(result);
				string = result.toString();

			}
		}
		System.out.println(string);
		return string;
	}
}
